package com.gcb.socket.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Timer;
import java.util.TimerTask;

public class MyServer {

	private ServerSocket server;
	private Socket client;
	private BufferedReader in;
	private PrintWriter out;
	private Timer timer = new Timer();
	private TimerTask task = new TimerTask() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			String str = "this is test";
			System.out.println(str);
			out.println("MyServer: " + str);
			//out.flush();
		}
	};
	
	public MyServer(){
		try {
			server = new ServerSocket(4433);
			while(true){
				client = server.accept();
				System.out.println("MyServer: conn succ");
				out = new PrintWriter(client.getOutputStream());
				PipedInputStream pis = new PipedInputStream();
				PipedOutputStream pus = new PipedOutputStream(pis);
				String str = "this is test";
				System.out.println(str);
				out.println("MyServer: " + str);
				out.write("s");
				out.flush();
				//while(true){
					//String str = in.readLine();
					
					//pus.write(str.getBytes());
					//timer.scheduleAtFixedRate(task, 0, 2000);
				
				//}
				//client.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @param args void
	 * @author Creat at 2011-12-26
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new MyServer();
	}
}
